package org.wishwork.api.order.query;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.wishwork.api.ExtendBase;
import org.wishwork.exception.ExceptionEnum;
import org.wishwork.exception.JSTException;

import java.util.List;

/**
 * @description: 订单查询
 * @author: WangYX
 * @create: 2022-04-13 10:27
 * @Version: 1.0.0
 **/
@ToString
@Getter
@EqualsAndHashCode
public class OrderQueryRequest extends ExtendBase {

    /**
     * 非必填
     * 店铺编号
     */
    private Integer shopId;

    /**
     * 非必填
     * shop_id为0且is_offline_shop为true查询线下店铺单据
     */
    private Boolean isOfflineShop;

    /**
     * 非必填
     * 线上单号，最大限制20条
     */
    private List<String> soIds;

    /**
     * 非必填
     * 待付款：WaitPay；
     * 发货中：Delivering；
     * 被合并：Merged；
     * 异常：Question；
     * 被拆分：Split；
     * 等供销商|外仓发货：WaitOuterSent；
     * 已付款待审核：WaitConfirm；
     * 已客审待财审：WaitFConfirm；
     * 已发货：Sent；
     * 取消：Cancelled
     */
    private String status;

    protected OrderQueryRequest(Integer pageIndex, Integer pageSize, String modifiedBegin, String modifiedEnd,
                                Integer shopId, Boolean isOfflineShop, List<String> soIds, String status) {
        super(pageIndex, pageSize, modifiedBegin, modifiedEnd);
        this.shopId = shopId;
        this.isOfflineShop = isOfflineShop;
        this.soIds = soIds;
        this.status = status;
    }


    public static Builder builder() {
        return new Builder();
    }

    @Override
    public Builder toBuilder() {
        return new Builder().pageIndex(this.pageIndex).pageSize(this.pageSize)
                .modifiedBegin(this.modifiedBegin).modifiedEnd(this.modifiedEnd)
                .shopId(this.shopId).isOfflineShop(this.isOfflineShop).soIds(this.soIds)
                .status(this.status);
    }


    public static class Builder extends ExtendBase.Builder<Builder, OrderQueryRequest> {
        private Integer shopId;
        private Boolean isOfflineShop;
        private List<String> soIds;
        private String status;

        @Override
        public Builder modifiedBegin(String modifiedBegin) {
            this.modifiedBegin = modifiedBegin;
            return this;
        }

        @Override
        public Builder modifiedEnd(String modifiedEnd) {
            this.modifiedEnd = modifiedEnd;
            return this;
        }

        @Override
        public Builder pageIndex(Integer pageIndex) {
            this.pageIndex = pageIndex;
            return this;
        }

        @Override
        public Builder pageSize(Integer pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        public Builder shopId(Integer shopId) {
            this.shopId = shopId;
            return this;
        }

        public Builder isOfflineShop(Boolean isOfflineShop) {
            this.isOfflineShop = isOfflineShop;
            return this;
        }

        public Builder soIds(List<String> soIds) {
            if (soIds != null) {
                if (soIds.size() > 20) {
                    throw new JSTException(ExceptionEnum.USER_ERROR_A0420, "线上单号，最大限制20条");
                }
            }
            this.soIds = soIds;
            return this;
        }

        public Builder status(String status) {
            this.status = status;
            return this;
        }

        @Override
        public OrderQueryRequest build() {
            this.pageIndex = this.pageIndex == null ? 1 : this.pageIndex < 0 ? 1 : this.pageIndex;
            this.pageSize = this.pageSize == null ? 30 : this.pageSize < 0 ? 30 : this.pageSize;
            if (this.soIds == null || this.soIds.size() <= 0) {
                keyIsEmptyTimeHandle(7);
            }
            modifiedTimeHandle(7);
            return new OrderQueryRequest(this.pageIndex, this.pageSize, this.modifiedBegin, this.modifiedEnd,
                    this.shopId, this.isOfflineShop, this.soIds, this.status);
        }
    }


}
